package com.bionic.banking.commands;

import com.bionic.banking.auth.manager.Config;
import com.bionic.banking.auth.manager.Message;
import com.bionic.banking.core.entity.BankAccount;
import com.bionic.banking.core.entity.Currency;
import com.bionic.banking.dao.connection.LoggerLoader;
import com.bionic.banking.logic.DomainUtil;
import com.bionic.banking.logic.controller.BankAccountController;
import com.bionic.banking.logic.exception.CurrencyNotFoundException;
import java.math.BigInteger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CommandCreateBankAccount implements ICommand {

    private static final org.apache.log4j.Logger logger =
            LoggerLoader.getLogger(CommandCreateBankAccount.class);

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        String page = null;
        try {
            Integer customerId = Integer.valueOf(request.getParameter("customerId"));
            String type = (String) request.getParameter("accountType");
            String description = (String) request.getParameter("description");
            BigInteger balance = new BigInteger(request.getParameter("balance"));
            BigInteger creditAvailable = new BigInteger(request.getParameter("creditLine"));
            Integer currencyCode = Integer.valueOf(request.getParameter("currency"));
            Currency currency = DomainUtil.getCurrencyByCode(currencyCode);
            if (currency == null) {
                throw new CurrencyNotFoundException("Currency code='" + currencyCode + "' doesn't exists");
            }

            //Create a BankAccount entity
            BankAccount entity = new BankAccount(type, currencyCode, balance, creditAvailable, description, customerId);
            
            //Use session bean to commit newly created entity into DB               
            BankAccountController bean = new BankAccountController();

            if (bean.create(entity)) {
                request.setAttribute("bankaccount", entity);
                //page = Config.getInstance().getProperty(Config.BANKACCOUNTLIST);
                request.setAttribute("content", Config.getInstance().getProperty(Config.BANKACCOUNTLIST));
                page = Config.getInstance().getProperty(Config.MAIN);
            } else {
                request.setAttribute("error",
                        Message.getInstance().getProperty(Message.CREATE_BANKACCOUNT_ERROR));
                page = Config.getInstance().getProperty(Config.ERROR);
            }
        } catch (CurrencyNotFoundException ex) {
            logger.error(ex);
            request.setAttribute("error",
                    Message.getInstance().getProperty(Message.CURRENCY_NOTFOUND_ERROR));
            page = Config.getInstance().getProperty(Config.ERROR);
        } catch (InstantiationException | IllegalAccessException ex) {
            logger.error(ex);
            request.setAttribute("error", ex.getMessage());
            page = Config.getInstance().getProperty(Config.ERROR);
        } catch (Exception ex) {
            logger.error(ex);
            request.setAttribute("error", ex.getMessage());
            page = Config.getInstance().getProperty(Config.ERROR);
        }
        return page;
    }
}
